/**
 ******************************************************************************
 * @file    lptmr.h
 * @author  hyseim software Team
 * @date    21-Mar-2024
 * @brief   This file provides all the headers of the lptmr functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LPTMR_H__
#define __LPTMR_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "chip_define.h"
#include "utils.h"

/* ================================================================================ */
/* ================             Low Power Timer (LPTMR)            ================ */
/* ================================================================================ */
typedef struct
{
    __IO uint32_t CSR;          /*!< Low Power Timer Control Status Register,          Address offset: 0x000 */
    __IO uint32_t PSR;          /*!< Low Power Timer Prescale Register,                Address offset: 0x004 */
    __IO uint32_t CMR;          /*!< Low Power Timer Compare Register,                 Address offset: 0x008 */
    __I  uint32_t CNR;          /*!< Low Power Timer Counter Register,                 Address offset: 0x00C */
} LPTMR_t;

#define LPTMR            ((LPTMR_t *) LPTIMER_BASE)
/* ================================================================================ */
/* ================             Low Power Timer (LPTMR)            ================ */
/* ================================================================================ */
/********************************  Bit definition for LPTMR_CSR register  ********************************/
#define LPTMR_CSR_TEN_MASK              (0x1U << 0)         /*!< LPTMR Enable */

#define LPTMR_CSR_TMS_MASK              (0x1U << 1)         /*!< LPTMR Mode Select */
#define LPTMR_CSR_TMS_TIME              (0x0U << 1)         /*!< LPTMR Time Counter Mode */
#define LPTMR_CSR_TMS_PULSE             (0x1U << 1)         /*!< LPTMR Pulse Counter Mode */

#define LPTMR_CSR_TFC_MASK              (0x1U << 2)         /*!< LPTMR Free-Running Counter */
#define LPTMR_CSR_TFC_WRAP              (0x0U << 2)         /*!< LPTMR CNR is reset whenever TCF is set */
#define LPTMR_CSR_TFC_FREERUNNING       (0x1U << 2)         /*!< LPTMR CNR is reset on overflow */

#define LPTMR_CSR_TPP_MASK              (0x1U << 3)         /*!< LPTMR Pin Polarity */
#define LPTMR_CSR_TPP_ACTIVE_HIGH       (0x0U << 3)         /*!< LPTMR Pulse Counter input source is active-high */
#define LPTMR_CSR_TPP_ACTIVE_LOW        (0x1U << 3)         /*!< LPTMR Pulse Counter input source is active-low */

#define LPTMR_CSR_TIE_MASK              (0x1U << 6)         /*!< LPTMR Interrupt Enable */
#define LPTMR_CSR_TCF_MASK              (0x1U << 7)         /*!< LPTMR Compare Flag */
#define LPTMR_CSR_TDRE_MASK             (0x1U << 8)         /*!< LPTMR DMA Request Enable */

/********************************  Bit definition for LPTMR_PSR register  ********************************/
#define LPTMR_PSR_PBYP_MASK             (0x1U << 2)         /*!< LPTMR Prescaler Bypass */
#define LPTMR_PSR_PRESCALE_MASK         (0xfU << 3)         /*!< LPTMR Prescale Value */

/** @addtogroup IM110GW_LPTMR_Driver
  * @{
  */

/** @addtogroup LPTMR
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  LPTMR Prescaler Selection  
  */
typedef enum {
    LPTMR_PRESCALE_2                        = 0x00u << 3,     /*!< Timer mode: prescaler 2,     Glitch filter mode: invalid */
    LPTMR_PRESCALE_4_GLITCHFILTER_2         = 0x01u << 3,     /*!< Timer mode: prescaler 4,     Glitch filter mode: 2 clocks */
    LPTMR_PRESCALE_8_GLITCHFILTER_4         = 0x02u << 3,     /*!< Timer mode: prescaler 8,     Glitch filter mode: 4 clocks */
    LPTMR_PRESCALE_16_GLITCHFILTER_8        = 0x03u << 3,     /*!< Timer mode: prescaler 16,    Glitch filter mode: 8 clocks */
    LPTMR_PRESCALE_32_GLITCHFILTER_16       = 0x04u << 3,     /*!< Timer mode: prescaler 32,    Glitch filter mode: 16 clocks */
    LPTMR_PRESCALE_64_GLITCHFILTER_32       = 0x05u << 3,     /*!< Timer mode: prescaler 64,    Glitch filter mode: 32 clocks */
    LPTMR_PRESCALE_128_GLITCHFILTER_64      = 0x06u << 3,     /*!< Timer mode: prescaler 128,   Glitch filter mode: 64 clocks */
    LPTMR_PRESCALE_256_GLITCHFILTER_128     = 0x07u << 3,     /*!< Timer mode: prescaler 256,   Glitch filter mode: 128 clocks */
    LPTMR_PRESCALE_512_GLITCHFILTER_256     = 0x08u << 3,     /*!< Timer mode: prescaler 512,   Glitch filter mode: 256 clocks */
    LPTMR_PRESCALE_1024_GLITCHFILTER_512    = 0x09u << 3,     /*!< Timer mode: prescaler 1024,  Glitch filter mode: 512 clocks */
    LPTMR_PRESCALE_2048_GLITCHFILTER_1024   = 0x0Au << 3,     /*!< Timer mode: prescaler 2048,  Glitch filter mode: 1024 clocks */
    LPTMR_PRESCALE_4096_GLITCHFILTER_2048   = 0x0Bu << 3,     /*!< Timer mode: prescaler 4096,  Glitch filter mode: 2048 clocks */
    LPTMR_PRESCALE_8192_GLITCHFILTER_4096   = 0x0Cu << 3,     /*!< Timer mode: prescaler 8192,  Glitch filter mode: 4096 clocks */
    LPTMR_PRESCALE_16384_GLITCHFILTER_8192  = 0x0Du << 3,     /*!< Timer mode: prescaler 16384, Glitch filter mode: 8192 clocks */
    LPTMR_PRESCALE_32768_GLITCHFILTER_16384 = 0x0Eu << 3,     /*!< Timer mode: prescaler 32768, Glitch filter mode: 16384 clocks */
    LPTMR_PRESCALE_65536_GLITCHFILTER_32768 = 0x0Fu << 3      /*!< Timer mode: prescaler 65536, Glitch filter mode: 32768 clocks */
} LPTMR_Prescaler_t;

/** 
  * @brief  LPTMR Init Structure definition  
  */
typedef struct
{
    uint16_t LPTMR_WorkMode;            /*!< Time/Pulse Counter Mode 
                                        This parameter can be a value of @ref LPTMR_WorkMode */

    uint16_t LPTMR_CountMode;           /*!< Enable/Disable Free Running Mode 
                                        This parameter can be a value of @ref LPTMR_CountMode */

    uint16_t LPTMR_PinPolarity;         /*!< Pin Polarity for Pulse-Counter
                                        This parameter can be a value of @ref LPTMR_PinPolarity */

    uint16_t LPTMR_InterruptEnable;     /*!< Enable/Disable Interrupt 
                                        This parameter can be a value of @ref LPTMR_InterruptGeneration */

    uint16_t LPTMR_DmaRequest;          /*!< Enable/Disable DMA requests 
                                        This parameter can be a value of @ref LPTMR_DmaRequest */

    uint16_t LPTMR_BypassPrescaler;     /*!< Enable/Disable prescaler bypass 
                                        This parameter can be a value of @ref LPTMR_Prescaler_Bypass */

    LPTMR_Prescaler_t LPTMR_Prescaler;  /*!< Prescaler Selection 
                                        This parameter can be a value of @ref LPTMR Prescaler Selection */
    
    uint16_t LPTMR_CompareValue;        /*!< Compare value */

} LPTMR_Init_t;

/* Exported constants --------------------------------------------------------*/

/** @defgroup LPTMR_Exported_Constants
  * @{
  */

/** @defgroup LPTMR_WorkMode
  * @{
  */
#define LPTMR_Time_Mode                 LPTMR_CSR_TMS_TIME
#define LPTMR_Pulse_Mode                LPTMR_CSR_TMS_PULSE
/**
  * @}
  */


/** @defgroup LPTMR_CountMode
  * @{
  */
#define LPTMR_Count_Wrap                LPTMR_CSR_TFC_WRAP
#define LPTMR_Count_FreeRunning         LPTMR_CSR_TFC_FREERUNNING
/**
  * @}
  */


/** @defgroup LPTMR_PinPolarity
  * @{
  */
#define LPTMR_Polarity_High             LPTMR_CSR_TPP_ACTIVE_HIGH
#define LPTMR_Polarity_Low              LPTMR_CSR_TPP_ACTIVE_LOW
/**
  * @}
  */


/** @defgroup LPTMR_InterruptGeneration
  * @{
  */
#define LPTMR_Interrupt_Enable          LPTMR_CSR_TIE_MASK
#define LPTMR_Interrupt_Disable         (0x0U << 6)
/**
  * @}
  */


/** @defgroup LPTMR_DmaRequest
  * @{
  */
#define LPTMR_DmaRequest_Enable         LPTMR_CSR_TDRE_MASK
#define LPTMR_DmaRequest_Disable        (0x0U << 8)
/**
  * @}
  */


/** @defgroup LPTMR_Prescaler_Bypass
  * @{
  */
#define LPTMR_Prescaler_Enable          (0x0U << 2)
#define LPTMR_Prescaler_Disable         LPTMR_PSR_PBYP_MASK
/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

void LPTMR_Init(LPTMR_Init_t *LPTMR_InitStruct);
void LPTMR_StructInit(LPTMR_Init_t *LPTMR_StructInit);
void LPTMR_Enable(void);
void LPTMR_Disable(void);
FlagStatus_t LPTMR_GetDmaRequest(void);
FlagStatus_t LPTMR_GetCompareFlag(void);
void LPTMR_ClearCompareFlag(void);
LPTMR_Prescaler_t LPTMR_GetPrescaler(void);
uint16_t LPTMR_GetCompareValue(void);
void LPTMR_SetCompareValue(uint16_t CompareValue);
uint16_t LPTMR_GetCounterValue(void);

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif
